package com.aiyige.page.my.localVideo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.utils.BitmapUtils;
import com.aiyige.utils.TimeUtils;
import com.bumptech.glide.Glide;

import java.util.List;


/**
 * 查询视频的全部文件，视频、图片、音频
 * @author Administrator
 *
 */
public class AllFilesActivity extends BaseActivity implements OnClickListener {
	public static final String INTENT_SINGLE_SELECT = "single_select";
	public static final String INTENT_TYPE = "type";

	public static final String TYPE_VIDEO = "video";
	public static final String TYPE_AUDIO = "audio";
	public static final String TYPE_IMAGE = "image";

	private TextView titleView;
	private TextView finishButton;
	private ListView listView;
	private TextView nodataView;
	private FileAdapter adapter;

	private List<SysFileInfo> list;
	private boolean isSingleSelect;
	private String type;

	private String mineType;

	private SysFileHelper helper;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (isFinishing()) {
				return;
			}
			hideLoading();
			if (null != list && !list.isEmpty()) {
				listView.setAdapter(adapter);
				nodataView.setVisibility(View.GONE);
			} else {
				nodataView.setVisibility(View.VISIBLE);
			}
		};
	};
	
	
	private OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
								long arg3) {
			SysFileInfo info = list.get(pos);
			if (isSingleSelect) { // 单选
				Intent data = new Intent();
				data.putExtra("data", info);
				setResult(RESULT_OK, data);
				finish();
			}else { // 多选
				info.setSelect(!info.isSelect());
				adapter.notifyDataSetChanged();
				int count = getSelectedCount();
				if (count == 0) {
					finishButton.setText(R.string.finish);
				}else {
					finishButton.setText(getString(R.string.finish) + "("
							+ count + ")");
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.page_all_files);
		initView();
		initData();
	}
	

	private void initView() {
		titleView = (TextView) findViewById(R.id.title_tv);
		finishButton = (TextView) findViewById(R.id.finish);

		listView = (ListView) findViewById(R.id.list);
		adapter = new FileAdapter();

		nodataView = (TextView) findViewById(R.id.nodata);

		listView.setOnItemClickListener(itemClickListener);
		findViewById(R.id.back_iv).setOnClickListener(this);
		finishButton.setOnClickListener(this);
	}

	private void initData() {
		isSingleSelect = getIntent().getBooleanExtra(INTENT_SINGLE_SELECT,
				false);
		type = getIntent().getStringExtra(INTENT_TYPE);
		
		finishButton.setVisibility(isSingleSelect ? View.GONE : View.VISIBLE);
		
		if (type.equals(TYPE_AUDIO)) {
			titleView.setText(R.string.select_audio);
			nodataView.setText(R.string.nodata_audio);
			mineType = "audio/*";
		} else if (type.equals(TYPE_IMAGE)) {
			titleView.setText(R.string.select_photo);
			nodataView.setText(R.string.nodata_image);
			mineType = "image/*";
		} else if (type.equals(TYPE_VIDEO)) {
			titleView.setText(R.string.select_video);
			nodataView.setText(R.string.nodata_video);
			mineType = "video/*";
		}
		helper = new SysFileHelper(this);

		showLoading();
		loadData();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_iv:
			setResult(RESULT_CANCELED);
			finish();
			break;
		case R.id.finish:
			onSelectFinish();
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		super.onBackPressed();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		helper.clear();
	}

	/**
	 * 加载数据
	 */
	private void loadData() {
		new Thread() {
			@Override
			public void run() {
				if (TextUtils.isEmpty(type)) {
					return;
				}
				if (type.equals(TYPE_VIDEO)) {
					helper.getAllVideoThumbnails();
					list = helper.getAllVideos();
					getVideoCover();
				} else if (type.equals(TYPE_AUDIO)) {
					list = helper.getAllAudios();
				} else if (type.equals(TYPE_IMAGE)) {
					helper.getAllImageThumbnails();
					list = helper.getAllImages();
				}
				handler.sendEmptyMessage(0);
			}

		}.start();
	}
	
	private void getVideoCover() {
		for (int i = 0; i < list.size(); i++) {
			SysFileInfo info = list.get(i);
			if (null == info.getImgPath() && null != info.getPath()) {
				info.setImgPath(BitmapUtils.createVideoThumbnail(info.getPath()));
			}
		}
	}
	
	/**
	 * 返回选中数量
	 * @return
	 */
	private int getSelectedCount() {
		int count = 0;
		for (SysFileInfo info : list) {
			if (info.isSelect()) {
				count++;
			}
		}
		return count;
	}
	
	/**
	 * 选择完成
	 */
	private void onSelectFinish() {
		SysFileHelper.selectList.clear();
		for (SysFileInfo info : list) {
			if (info.isSelect()) {
				SysFileHelper.selectList.add(info);
			}
		}
		setResult(RESULT_OK);
		finish();
	}

	private class FileAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int pos, View view, ViewGroup group) {
			ViewHolder holder = null;
			if (null == view) {
				view = LayoutInflater.from(AllFilesActivity.this).inflate(
						R.layout.item_file_list, null);
				holder = new ViewHolder();
				holder.imgView = (ImageView) view.findViewById(R.id.image);
				holder.musicView = (ImageView) view
						.findViewById(R.id.ico_music);
				holder.nameView = (TextView) view.findViewById(R.id.name);
				holder.descView = (TextView) view.findViewById(R.id.desc);
				holder.selectedView = (ImageView) view
						.findViewById(R.id.selected);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

			SysFileInfo info = list.get(pos);
			holder.nameView.setText(info.getName());
			holder.imgView.setImageResource(R.drawable.default_image_bg);
			if (type.equals(TYPE_AUDIO)) {
				holder.musicView.setVisibility(View.VISIBLE);
				holder.imgView.setVisibility(View.GONE);
				holder.descView.setText(TimeUtils.getDurationTime(info
						.getDuration() / 1000));
			} else if (type.equals(TYPE_VIDEO)) {
				String thumbPath = helper.getThumbnailWithVideoID(info.getId(), info.getImgPath());
				info.setImgPath(thumbPath);
				Glide.with(AllFilesActivity.this).load(thumbPath).into(holder.imgView);
				
				holder.descView.setText(TimeUtils.getDurationTime(info
						.getDuration() / 1000));

			} else if (type.equals(TYPE_IMAGE)) {
				String thumbPath = helper.getThumbnailWithImageID(info.getId(), info.getPath());
				Glide.with(AllFilesActivity.this).load(thumbPath).into(holder.imgView);
				holder.descView.setText(CacheUtils.getFormatSize(2,
						info.getSize()));
			}

			holder.selectedView.setVisibility(isSingleSelect ? View.GONE
					: View.VISIBLE);

			holder.selectedView
					.setImageResource(info.isSelect() ? R.drawable.ico_checked
							: R.drawable.ico_uncheck);
			return view;
		}

		class ViewHolder {
			ImageView imgView;
			ImageView musicView;
			TextView nameView;
			TextView descView;
			ImageView selectedView;
		}
		
	}
}
